package com.sh.mytool.cv.controller;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.sh.common.extjs.CUDResult;
import com.sh.common.extjs.GetResult;
import com.sh.common.web.ResponseHandle;
import com.sh.mytool.cv.domain.Column;
import com.sh.mytool.cv.service.ColumnService;

@Controller
public class ColumnController {

    public ColumnService getColumnService() {
        return columnService;
    }

    public void setColumnService(ColumnService columnService) {
        this.columnService = columnService;
    }

    @Resource(name="columnService")
    private ColumnService columnService;

    @InitBinder
    public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }

    @RequestMapping(value = "/column/list", method = RequestMethod.POST)
    public void columnList(@RequestParam(value="parentId") Long parentId, HttpServletResponse response, PrintWriter out) throws Exception{
        List<Column> result = columnService.list(parentId);
        String json = ResponseHandle.handleJson("", "parent", result, false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/column/root", method = RequestMethod.POST)
    public void columnRoot(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        Column result = columnService.get(id);
        String json = ResponseHandle.handleJson("", "parent", result, false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/column/get", method = RequestMethod.POST)
    public void columnGet(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        Column result = columnService.get(id);
        Boolean isSuccess = false;
        if(result != null){
            isSuccess = true;
        }
        String json = ResponseHandle.handleJson("", "data.article,data.parent", new GetResult(isSuccess, result), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    // 尽可能用@RequestParam这样的组合，不要用BeanUtils.populate(node, params)或者Map
    @RequestMapping(value = "/column/edit", method = RequestMethod.POST)
    public void columnEdit(Column column, @RequestParam(value="parentId") Long parentId, HttpServletResponse response, PrintWriter out) throws Exception{
        boolean isSuccess = true;
        try {
            Long id = column.getId();
            if(id != null){
                columnService.update(column);
            }else{
                columnService.insert(column, parentId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/column/remove", method = RequestMethod.POST)
    public void columnRemove(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        boolean isSuccess = true;
        try {
            columnService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/column/move", method = RequestMethod.POST)
    public void columnMove(@RequestParam(value="id") Long id, @RequestParam(value="changeId") Long changeId, HttpServletResponse response, PrintWriter out) throws Exception{
        boolean isSuccess = true;
        try {
            columnService.move(changeId, id);
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }
    
}
